<?php 
	include($_SERVER['DOCUMENT_ROOT']."/headerins.php");
?>

<?php

try {


    //Getting records (listAction)
    if ($_GET["action"] == "list") {
        //Get records from database
        $result = mysql_query("
            SELECT 
            Id,
            Number, Email, Password, LastName, Name, 
            MiddleName, CreateDT, LastЕnterDT, 
            Active, ParentNumber
            FROM Users;");

        //Add all records to an array
        $rows = array();
        while ($row = mysql_fetch_array($result)) {
            $rows[] = $row;
        }

        //Return result to jTable
        $jTableResult = array();
        $jTableResult['Result'] = "OK";
        $jTableResult['Records'] = $rows;
        print json_encode($jTableResult);
    }
    //Creating a new record (createAction)
    else if ($_GET["action"] == "create") {
        //Insert record into database
        $result = mysql_query("
            INSERT INTO matgroup(
				name
            )VALUES(
                '" . $_GET["name"] . "'
            );");

        if (!$result) {
            die('Ошибка при внесении записи: ' . mysql_error());
        }else
            print('Запись успешно добавлена');
    }else if ($_GET["action"] == "update") {
        //Update record in database
        $result = mysql_query("UPDATE matgroup SET 
            Name        = '" . $_GET["name"] . "'
            WHERE Id = " . $_GET["id"] . ";");
//            Password    = '" . $_GET["Password"] . "',                    
//            Active      = '" . $_GET["Active"] . "',
//            ParentNumber = '" . $_GET["ParentNumber"] . "'
//            
        //Return result to jTable
//		$jTableResult = array();
//		$jTableResult['Result'] = "OK";
//		print json_encode($jTableResult);
//        if (!$result) {
//            die('Ошибка при изменении записи: ' . mysql_error());
//        }else
            print('Запись успешно изменена');
    }
    //Deleting a record (deleteAction)
    else if ($_GET["action"] == "delete") {
        //Delete from database
        $result = mysql_query("DELETE FROM matgroup WHERE Id = " . $_GET["id"] . ";");
        
//        if (!$result) {
//            die('Ошибка при изменении записи: ' . mysql_error());
//        }else
            print('Запись успешно удалена');
        //Return result to jTable
//        $jTableResult = array();
//        $jTableResult['Result'] = "OK";
//        print json_encode($jTableResult);
    }

    //Close database connection
    mysql_close($con);
} catch (Exception $ex) {
    //Return error message
//    $jTableResult = array();
//    $jTableResult['Result'] = "ERROR";
//    $jTableResult['Message'] = $ex->getMessage();
//    print json_encode($jTableResult);
    print($ex->getMessage());  
}
?>
